package nc.tb.app.vo;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import nc.vo.pub.SuperVO;

public class FmtData implements Serializable {
	private static final long serialVersionUID = 1000L;
	private final static String FMTKEY = "FmtRowData";
	private String pk_obj;
	private String pk_fmtview;
	private String pk_plan;
	private ArrayList<FmtRowData> rowData = new ArrayList<FmtRowData>();
	private HashMap<String,ArrayList<FmtRowData>> hmVarRowData = null;
	private transient boolean isChangedAfterSave = false;
	public void setDbVO(SuperVO vo) {
		SdPlanfmtdataVO dbvo = (SdPlanfmtdataVO) vo;
		this.pk_obj = dbvo.getPk_obj();
		this.pk_fmtview = dbvo.getPk_fmtview();
		this.pk_plan = dbvo.getPk_plan();	
//		this.rowData = (ArrayList<FmtRowData>) dbvo.getFmtdata();
		try {
			HashMap<String,ArrayList<FmtRowData>> tmp = (HashMap<String,ArrayList<FmtRowData>>) dbvo.getFmtdata();;
			if (tmp != null) {
				rowData = tmp.remove(FMTKEY);
				if (!tmp.isEmpty()) {
					hmVarRowData = new HashMap<String,ArrayList<FmtRowData>>();
					for (String key : tmp.keySet()) {
						hmVarRowData.put(key, tmp.get(key));
					}
				}
			}
		} catch (ClassCastException ce) {}
		isChangedAfterSave = false;
	}
	public SdPlanfmtdataVO toDbVO() {
		SdPlanfmtdataVO dbvo = new SdPlanfmtdataVO();
		dbvo.setPk_obj(pk_obj);
		dbvo.setPk_fmtview(pk_fmtview);
		dbvo.setPk_plan(pk_plan);
//		dbvo.setFmtdata(rowData);
		HashMap<String,ArrayList<FmtRowData>> tmp = new HashMap<String, ArrayList<FmtRowData>>();
		if (hmVarRowData != null) {
			for (String key : hmVarRowData.keySet()) {
				tmp.put(key, hmVarRowData.get(key));
			}
		}
		tmp.put(FMTKEY, rowData);
		dbvo.setFmtdata(tmp);
		isChangedAfterSave = false;
		return dbvo;
	}
	public String getPk_fmtview() {
		return pk_fmtview;
	}
	public void setPk_fmtview(String pk_fmtview) {
		this.pk_fmtview = pk_fmtview;
	}
	public String getPk_obj() {
		return pk_obj;
	}
	public void setPk_obj(String pk_obj) {
		this.pk_obj = pk_obj;
	}
	public String getPk_plan() {
		return pk_plan;
	}
	public void setPk_plan(String pk_plan) {
		this.pk_plan = pk_plan;
	}
	public ArrayList<FmtRowData> getRowData() {
		return rowData;
	}
	public void setRowData(ArrayList<FmtRowData> rowData) {
		this.rowData = rowData;
	}
//	public void addVarRowData(String varId, FmtRowData data) {
//		if (hmVarRowData == null)
//			hmVarRowData = new HashMap<String, ArrayList<FmtRowData>>();
//		ArrayList<FmtRowData> row = hmVarRowData.get(varId);
//		if (row == null) {
//			row = new ArrayList<FmtRowData>();
//			hmVarRowData.put(varId, row);
//		}
//		row.add(data);
//	}
	public HashMap<String, ArrayList<FmtRowData>> getVarRowData() {
		return hmVarRowData;
	}
	public void setVarRowData(HashMap<String, ArrayList<FmtRowData>> hm) {
		hmVarRowData = hm;
	}
	public boolean isVarFmtData() {
		return hmVarRowData != null;
	}
	public boolean isEmpty() {
		if (rowData != null && rowData.size() > 0)
			return false;
		else if (hmVarRowData != null && !hmVarRowData.isEmpty())
			return false;
		return true;
	}
	public boolean isChangedAfterSave() {
		return isChangedAfterSave;
	}
	public void setChangedAfterSave(boolean isChangedAfterSave) {
		this.isChangedAfterSave = isChangedAfterSave;
	}
}
